package com.example.springbootchapter09.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

@Service
public class AsyncService {
    @Async
    public void noReturnAsyncTask() {
        try {
            // 模拟耗时操作
            Thread.sleep(3000);
            System.out.println("无返回值异步任务执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Async
    public Future<String> returnAsyncTask() {
        try {
            // 模拟耗时操作
            Thread.sleep(3000);
            return new java.util.concurrent.FutureTask<>(() -> "有返回值异步任务执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
            return new java.util.concurrent.FutureTask<>(() -> "任务执行出错");
        }
    }
}